package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsDataSource;
import cn.chiship.framework.upms.biz.system.entity.UpmsDataSourceExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsDataSourceMapper;
import cn.chiship.framework.upms.biz.system.service.UpmsDataSourceService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 三方数据库业务接口实现层
 * 2022/2/6
 *
 * @author lijian
 */
@Service
public class UpmsDataSourceServiceImpl extends BaseServiceImpl<UpmsDataSource, UpmsDataSourceExample> implements UpmsDataSourceService {


    @Resource
    UpmsDataSourceMapper upmsDataSourceMapper;

    @Override
    public BaseResult insertSelective(UpmsDataSource upmsDataSource) {
        UpmsDataSourceExample upmsDataSourceExample = new UpmsDataSourceExample();
        upmsDataSourceExample.createCriteria().andCodeEqualTo(upmsDataSource.getCode());
        if (upmsDataSourceMapper.countByExample(upmsDataSourceExample) == 0) {
            return super.insertSelective(upmsDataSource);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "编号已存在，请重新输入");
        }
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsDataSource upmsDataSource) {
        UpmsDataSourceExample upmsDataSourceExample = new UpmsDataSourceExample();
        upmsDataSourceExample.createCriteria().andCodeEqualTo(upmsDataSource.getCode()).andIdNotEqualTo(upmsDataSource.getId());
        if (upmsDataSourceMapper.countByExample(upmsDataSourceExample) == 0) {
            return super.updateByPrimaryKeySelective(upmsDataSource);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "编号已存在，请重新输入");
        }
    }
}
